package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.Reception;
import com.amsp.oa.oademo.service.ReceptionService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/reception")
public class ReceptionController {

    @Autowired
    private ReceptionService receptionService;

    @GetMapping
    public ResponseEntity<IPage<Reception>> getReceptionList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String status) {
        Page<Reception> pageParam = new Page<>(page, size);
        return ResponseEntity.ok(receptionService.getReceptionList(pageParam, keyword, status));
    }

    @GetMapping("/my")
    public ResponseEntity<IPage<Reception>> getMyReceptions(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam String applicant) {
        Page<Reception> pageParam = new Page<>(page, size);
        return ResponseEntity.ok(receptionService.getMyReceptions(pageParam, applicant));
    }

    @GetMapping("/{id}")
    public ResponseEntity<Reception> getReception(@PathVariable Long id) {
        return ResponseEntity.ok(receptionService.getById(id));
    }

    @GetMapping("/no/{applicationNo}")
    public ResponseEntity<Reception> getReceptionByNo(@PathVariable String applicationNo) {
        return ResponseEntity.ok(receptionService.getReceptionByNo(applicationNo));
    }

    @PostMapping
    public ResponseEntity<Reception> createReception(@RequestBody Reception reception) {
        return ResponseEntity.ok(receptionService.createReception(reception));
    }

    @PutMapping("/{id}")
    public ResponseEntity<Reception> updateReception(
            @PathVariable Long id,
            @RequestBody Reception reception) {
        reception.setId(id);
        return ResponseEntity.ok(receptionService.updateReception(reception));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteReception(@PathVariable Long id) {
        receptionService.deleteReception(id);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/{id}/approve")
    public ResponseEntity<Reception> approveReception(@PathVariable Long id) {
        Reception reception = receptionService.getById(id);
        reception.setStatus("approved");
        return ResponseEntity.ok(receptionService.updateReception(reception));
    }

    @PostMapping("/{id}/reject")
    public ResponseEntity<Reception> rejectReception(@PathVariable Long id) {
        Reception reception = receptionService.getById(id);
        reception.setStatus("rejected");
        return ResponseEntity.ok(receptionService.updateReception(reception));
    }

    @PostMapping("/submit")
    public ResponseEntity<Reception> submitReception(@RequestBody Reception reception) {
        return ResponseEntity.ok(receptionService.submitReception(reception));
    }
} 